package sol.leetcode;

import def.TreeNode;

/**
 * Convert Sorted Array to Binary Search Tree
 */
public class Issue108 {
    public TreeNode sortedArrayToBST(int[] nums) {
        return create(nums, 0, nums.length - 1);
    }

    private TreeNode create(int[] nums, int start, int end) {
        if (start > end)
            return null;
        int mid = start + (end - start) / 2;

        TreeNode root = new TreeNode(nums[mid]);
        root.left = create(nums, start, mid - 1);
        root.right = create(nums, mid + 1, end);

        return root;
    }

    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4};
        TreeNode root = new Issue108().sortedArrayToBST(nums);
        System.out.println(root.val);
    }
}
